

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="叶飞">
  <meta name="keywords" content="">
  
    <meta name="description" content="1 路由种类  静态路由  框架&#x2F;用户提前生成一个路由表，一般是map结构，key为URI上的path，value为代码执行点。 优点：只需要读取map，没有任何开销，速度奇快。 缺点：无法正则匹配路由，只能一一对应，模糊匹配的场景无法使用。  动态路由  用户定义好路由匹配规则，框架匹配路由时，根据规则动态去规划路由。 优点：适应性强，解决了静态路由的缺点。 缺点：相比静态路由有开销，具体视算">
<meta property="og:type" content="article">
<meta property="og:title" content="Gin动态路由">
<meta property="og:url" content="http://example.com/2022/04/17/20220417-Gin%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1/index.html">
<meta property="og:site_name" content="博客">
<meta property="og:description" content="1 路由种类  静态路由  框架&#x2F;用户提前生成一个路由表，一般是map结构，key为URI上的path，value为代码执行点。 优点：只需要读取map，没有任何开销，速度奇快。 缺点：无法正则匹配路由，只能一一对应，模糊匹配的场景无法使用。  动态路由  用户定义好路由匹配规则，框架匹配路由时，根据规则动态去规划路由。 优点：适应性强，解决了静态路由的缺点。 缺点：相比静态路由有开销，具体视算">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/img/20220417-Gin%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1.assets/v2-89fd7205fcf98f5e4570da85a37573d6_720w.jpg">
<meta property="og:image" content="http://example.com/img/20220417-Gin%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1.assets/v2-ba8540f8a1743700087d42c9e2b2e385_720w.jpg">
<meta property="article:published_time" content="2022-04-17T14:02:12.000Z">
<meta property="article:modified_time" content="2022-04-17T14:12:21.400Z">
<meta property="article:author" content="叶飞">
<meta property="article:tag" content="Gin">
<meta property="article:tag" content="动态路由">
<meta property="article:tag" content="前缀树">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://example.com/img/20220417-Gin%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1.assets/v2-89fd7205fcf98f5e4570da85a37573d6_720w.jpg">
  
  
  
  <title>Gin动态路由 - 博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>No Tomorrow</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Gin动态路由"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        叶飞
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-04-17 22:02" pubdate>
          星期日, 四月 17日 2022, 10:02 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          <!-- compatible with older versions-->
          2.1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          <!-- compatible with older versions-->
          18 分钟
        
      </span>
    

    
    
      
        <span id="leancloud-page-views-container" class="post-meta" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="leancloud-page-views"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Gin动态路由</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="1-路由种类"><a class="markdownIt-Anchor" href="#1-路由种类"></a> 1 路由种类</h2>
<ul>
<li>静态路由</li>
</ul>
<p>框架/用户提前生成一个路由表，一般是map结构，key为URI上的path，value为代码执行点。</p>
<p>优点：只需要读取map，没有任何开销，速度奇快。</p>
<p>缺点：无法正则匹配路由，只能一一对应，模糊匹配的场景无法使用。</p>
<ul>
<li>动态路由</li>
</ul>
<p>用户定义好路由匹配规则，框架匹配路由时，根据规则动态去规划路由。</p>
<p>优点：适应性强，解决了静态路由的缺点。</p>
<p>缺点：相比静态路由有开销，具体视算法和路由匹配规则而定</p>
<h2 id="2-gin动态路由前缀树路由"><a class="markdownIt-Anchor" href="#2-gin动态路由前缀树路由"></a> 2 Gin动态路由–前缀树路由</h2>
<h3 id="21-使用示例"><a class="markdownIt-Anchor" href="#21-使用示例"></a> 2.1 使用示例</h3>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs go">r := gin.New()<br>r.GET(<span class="hljs-string">&quot;/user/:name&quot;</span>, routeUser)<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">routeUser</span><span class="hljs-params">(c *gin.Context)</span></span>&#123;<br>    <span class="hljs-comment">//todo something</span><br>&#125;<br></code></pre></td></tr></table></figure>
<ul>
<li>定义了动态路由：<code>/usr/:name</code>，不会匹配<code>/user/</code>、<code>/user/allen/abc</code></li>
</ul>
<h3 id="22-初始化框架"><a class="markdownIt-Anchor" href="#22-初始化框架"></a> 2.2 初始化框架</h3>
<p><code>r := gin.New()</code>生成了一个<code>Engine</code>对象，<code>Engine</code>对象是整个框架的核心，也包含了对路由的操作和许多成员变量，其中包括路由要执行的任务链<code>Handlers HandlersChain</code>，方法树<code>trees methodTrees</code>等。</p>
<h3 id="23-定义路由"><a class="markdownIt-Anchor" href="#23-定义路由"></a> 2.3 定义路由</h3>
<p><code>r.GET(&quot;/user/:name&quot;, routeUser)</code>定义一个<code>GET</code>请求，模糊匹配<code>/user/:name</code>。</p>
<ul>
<li>步骤1：</li>
</ul>
<p>源码：<code>routergroup.go=&gt;group.calculateAbsolutePath()</code></p>
<p>将相对路径<code>/user/:name</code>join为绝对路径，因为有可能是个路由组，前面还有一些路径。路由组稍后再介绍。</p>
<ul>
<li>步骤2：</li>
</ul>
<p>源码：<code>routergroup.go=&gt;group.combineHandlers()</code></p>
<p>判断<code>handlersChain</code>的长度，不能超过<code>math.MaxInt8 / 2</code>并且把路由方法装载到<code>handlersChain</code>里面去。</p>
<ul>
<li>步骤3：</li>
</ul>
<p>源码：<code>routergroup.go=&gt;group.engine.addRoute()</code></p>
<p>装入路由存入<code>engine.trees</code>变量。</p>
<ul>
<li>步骤4：</li>
</ul>
<p>返回当前对象，达到能使用链式操作的目的。</p>
<h3 id="24-访问路由"><a class="markdownIt-Anchor" href="#24-访问路由"></a> 2.4 访问路由</h3>
<p>输入<code>localhost:8080/user/abc</code>，首先进入net的<code>ServeHTTP()</code>方法。然后被gin框架<code>handleHTTPRequest()</code>方法接受。循环之前注册的路由树<code>engine.tress</code>。</p>
<p>匹配method部分源码<code>gin.go=&gt;handleHTTPRequest()</code>：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs go">t := engine.trees<br><span class="hljs-keyword">for</span> i, tl := <span class="hljs-number">0</span>, <span class="hljs-built_in">len</span>(t); i &lt; tl; i++ &#123;<br>    <span class="hljs-keyword">if</span> t[i].method != httpMethod &#123;<br>        <span class="hljs-keyword">continue</span>    <span class="hljs-comment">//判断请求method是否相等如果不等则continue</span><br>    &#125;<br>    root := t[i].root<br>    <span class="hljs-comment">// Find route in tree</span><br>    value := root.getValue(rPath, c.params, unescape)<br>    <span class="hljs-comment">//匹配路由的核心算法，如果匹配出来的value为空则直接退出。</span><br>    ...<br>    ...<br>&#125;<br></code></pre></td></tr></table></figure>
<p><strong>匹配路由核心算法</strong>源码<code>tree.go=&gt;getValue()</code>：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(n *node)</span></span> getValue(path <span class="hljs-type">string</span>, params *Params, unescape <span class="hljs-type">bool</span>) (value nodeValue) &#123;<br>walk: <span class="hljs-comment">// Outer loop for walking the tree</span><br>    <span class="hljs-keyword">for</span> &#123;<br>        prefix := n.path<br>        <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(path) &gt; <span class="hljs-built_in">len</span>(prefix) &#123;<br>            <span class="hljs-keyword">if</span> path[:<span class="hljs-built_in">len</span>(prefix)] == prefix &#123;<br>                <span class="hljs-comment">//前缀匹配</span><br>                ...<br><br>                <span class="hljs-keyword">switch</span> n.nType &#123;<br>                <span class="hljs-keyword">case</span> param:<br>                ...<br>                (*value.params)[i] = Param&#123;<br>                    Key:   n.path[<span class="hljs-number">1</span>:],<br>                    Value: val,<br>                &#125;<span class="hljs-comment">// 匹配出参数</span><br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">if</span> path == prefix &#123;<br>            <span class="hljs-comment">// 完全匹配，无需匹配参数</span><br>            ...<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="25-前缀树算法"><a class="markdownIt-Anchor" href="#25-前缀树算法"></a> 2.5 前缀树算法</h3>
<p>前缀树的本质就是一棵<strong>查找树</strong>，有别于普通查找树，它适用于一些特殊场合，比如用于字符串的查找。</p>
<p>比如一个在路由的场景中，有1W个路由字符串，每个字符串长度不等，我们可以使用数组来储存，查找的时间复杂度是O(n)，可以用map来储存，查找的复杂度是O(1)，但是都没法解决动态匹配的问题，如果用前缀树时间复杂度是O(logn)，也可以解决动态匹配参数的问题。</p>
<p>下图展示了前缀树的原理，有以下6个字符串，如果要查找<code>cat</code>字符串，步骤如下：</p>
<ol>
<li>先拿字符<code>c</code>和<code>root</code>的第一个节点<code>a</code>比较，如果不等，再继续和父节点<code>root</code>的第二个节点比较，直到找到<code>c</code>。</li>
<li>再拿字符<code>a</code>和父节点<code>c</code>的第一个节点<code>a</code>比较，结果相等，则继续往下。</li>
<li>再拿字符<code>t</code>和父节点<code>a</code>的第一个节点<code>t</code>比较，结果相等，则完成。</li>
</ol>
<p><img src="/img/20220417-Gin%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1.assets/v2-89fd7205fcf98f5e4570da85a37573d6_720w.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<p>同理，在路由中，前缀树可以规划成如下：</p>
<p><img src="/img/20220417-Gin%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1.assets/v2-ba8540f8a1743700087d42c9e2b2e385_720w.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<p>具体查找方法和上面一致。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/Golang/" class="category-chain-item">Golang</a>
  
  
    <span>></span>
    
  <a href="/categories/Golang/Gin/" class="category-chain-item">Gin</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Gin/">#Gin</a>
      
        <a href="/tags/%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1/">#动态路由</a>
      
        <a href="/tags/%E5%89%8D%E7%BC%80%E6%A0%91/">#前缀树</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Gin动态路由</div>
      <div>http://example.com/2022/04/17/20220417-Gin动态路由/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>叶飞</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年4月17日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/04/18/20220418-httprouter/" title="httprouter">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">httprouter</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/04/17/20220417-Restful/" title="Restful实践">
                        <span class="hidden-mobile">Restful实践</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  


  
  









    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  
      <script>
        if (!window.MathJax) {
          window.MathJax = {
            tex    : {
              inlineMath: { '[+]': [['$', '$']] }
            },
            loader : {
              load: ['ui/lazy']
            },
            options: {
              renderActions: {
                insertedScript: [200, () => {
                  document.querySelectorAll('mjx-container').forEach(node => {
                    let target = node.parentNode;
                    if (target.nodeName.toLowerCase() === 'li') {
                      target.parentNode.classList.add('has-jax');
                    }
                  });
                }, '', false]
              }
            }
          };
        } else {
          MathJax.startup.document.state(0);
          MathJax.texReset();
          MathJax.typeset();
          MathJax.typesetPromise();
        }
      </script>
    

  <script  src="https://lib.baomitu.com/mathjax/3.2.1/es5/tex-mml-chtml.js" ></script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="/js/leancloud.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
